#!/usr/local/bin/python
# -*- coding: utf-8 -*-



from sqlalchemy import Column, Integer, Float, and_, Text, Date
from sqlalchemy.orm import declarative_base

import init
import toolkit
from init import session


Base = declarative_base()

class IncomeStatementAlchemy(Base):
    '''
    类功能：收支表数据库映射表
    '''

    __tablename__ = "income_statement"

    id = Column(Integer, primary_key=True, autoincrement=True, comment="数据表id值")
    date_time = Column(Date, comment="数字格式的年份和月份时间")
    year = Column(Integer, comment="数据归属年份")
    month = Column(Integer, comment="数据归属月份")
    suject_name = Column(Text, comment="资产负债表类目所属名称")
    current_month_value = Column(Float, comment="当前月金额")
    total_value = Column(Float, comment="累计金额")



    def __init__(self,
                 year=2022,
                 month=1,
                 subjectName="",
                 currentMonthValue=0.0,
                 totalValue=0.0
                 ):
        self.date_time = toolkit.setTargetDate(year, month)
        self.year = year
        self.month = month
        self.suject_name = subjectName
        self.current_month_value = currentMonthValue
        self.total_value = totalValue


    def AddIncomeStatementItem(self):
        '''
        函数功能：添加一条数据
        :return:
        '''
        session.add(self)
        session.commit()

    def UpdateIncomeStatementtem(self):
        '''
        函数功能；更新一条数据
        :return:
        '''

    def SearchIncomeStatementByMonthSub(self, year, month, subjectPath):


        incomeStatementInfo = session.query(IncomeStatementAlchemy).filter(and_(IncomeStatementAlchemy.year == year,
                                                                        IncomeStatementAlchemy.month == month))\
                                                                    .filter(IncomeStatementAlchemy.suject_name.like(subjectPath+"%"))\
                                                                    .all()

        '''
        incomeStatementInfo = session.query(IncomeStatementAlchemy).filter(and_(IncomeStatementAlchemy.year == year,
                                                                                IncomeStatementAlchemy.month == month)).all()
        '''
        return incomeStatementInfo



    def UpdateAllDatetimeDataTemp(self):
        '''
        函数功能：临时更新收支表数据,用于填充date_time字段的数据
        :return:
        '''

        incomeStatementAlchemyList = session.query(IncomeStatementAlchemy).filter().all()

        for i in range(len(incomeStatementAlchemyList)):

            incomeStatementAlchemyItem = incomeStatementAlchemyList[i]
            incomeStatementAlchemyItem.date_time = toolkit.setTargetDate(incomeStatementAlchemyItem.year,
                                                                      incomeStatementAlchemyItem.month)

        session.commit()
        return



    def ShowData(self):
        '''
        函数功能：展示读取出来的数据
        :return:
        '''
        pass

    def DelIncomeStatementByMonth(self, year, month):
        '''
        函数功能，删除指定月份的张博数据
        :param year:
        :param month:
        :return:
        '''

        deleteIncomeStatementInfo = session.query(IncomeStatementAlchemy).filter(and_(IncomeStatementAlchemy.year==year,
                                                                     IncomeStatementAlchemy.month==month))
        deleteIncomeStatementInfo.delete()
        session.commit()





def main():

    incomeStatementAlchemy = IncomeStatementAlchemy()
    incomeStatementAlchemy.UpdateAllDatetimeDataTemp()

    '''
    incomeStatementAlchemy = IncomeStatementAlchemy(year = 2022,
                                          month = 7,
                                          subjectName="资产->自有->货币->银行存款->交通银行",
                                          currentMonthValue=100,
                                          totalValue=199
                                          )

    incomeStatementAlchemy.AddIncomeStatementItem()
    '''


#创建数据库表
IncomeStatementAlchemy.metadata.create_all(init.engine)


if __name__ == '__main__':
    main()